package main.java.com.lee.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/2/11
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        IntStream.rangeClosed(1, 10).forEach(i->{
            executorService.submit(() -> countDownTest(countDownLatch));
        });
        System.out.println(countDownLatch.getCount());
        System.out.println(countDownLatch.getCount());
        System.out.println(countDownLatch.getCount());
        countDownLatch.await();
        System.out.println(countDownLatch.getCount());
        executorService.shutdownNow();
    }

    private static void countDownTest(CountDownLatch countDownLatch) {
        System.out.println(Thread.currentThread().getName() + ": 执行结束");
        countDownLatch.countDown();
    }
}
